package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import java.net.URLEncoder;
/**
 * 
 * @author msjykf
 * 短信调度任务 取模板设置发送区间内 短信未发送并且模板无需审核或已经审核的数据
 * 发送通道：无限天利
select tk.smsno,tk.exno,tk.phone,tk.msg,tk.batch_id 
from sms_send_task tk 
left join sms_send_batch sb on tk.batch_id=sb.batch_id 
left join sms_template_search st on tk.sms_id=st.sms_id
left join sms_pipe sp on sp.pipe_id=st.pipe
left join triggermode tg on st.relation_id=tg.doctemplate_id
where tk.temp_guid is null and tk.state=0 and sb.state>3 and sb.checkstate in(0,2) and sb.batchstate=0 and nvl(st.pipe,tk.pipe)=3 and sp.pipe_state=1
and to_char(sysdate,'hh24:mi')>=nvl(tg.sendtime,'00:00') 
and to_char(sysdate,'hh24:mi')>=nvl(tg.definestarttime,'00:00') 
and to_char(sysdate,'hh24:mi')<=nvl(tg.defineendtime,'23:59') 
order by st.priority
 *
 */
public class SmsSendWxtlTask implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(SmsSendWxtlTask.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String body){
		try {
			PropertiesUtil pt= new PropertiesUtil();
			def userid = Base64.encode(URLEncoder.encode(pt.readValue("wxtl_userid"),"utf-8"),"utf-8"); //短信账号 先URLEncode编码后使用Base64算法加密
			def pwd = Base64.encode(URLEncoder.encode(pt.readValue("wxtl_pwd"),"utf-8"),"utf-8"); //短信密码  先URLEncode编码后使用Base64算法加密
			def productid = pt.readValue("wxtl_pid"); //通道组id
			def ipport = pt.readValue("sms_ip")+":"+pt.readValue("wxtl_port");
			def mobile = Base64.encode(URLEncoder.encode(map.get("phone"),"utf-8"),"utf-8");
			def content = Util.stringTrim(Base64.encode(URLEncoder.encode(map.get("msg"),"utf-8"),"utf-8"));
			def lcode = "";
			def ssid = map.get("exno");
			def batch_id = map.get("batch_id");
			def format =32;
			def sign = "";
			def smsno = map.get("smsno");
			def url = "http://"+ipport+"/communication/sendSms.ashx?cid="+userid+"&pwd="+pwd+"&productid="+productid+"&mobile="+mobile+"&content="+content+"&lcode="+lcode+"&ssid="+ssid+"&format="+format+"&sign="+sign+"&custom="+smsno;
			def charset = "utf-8"; //设置字符集
			def timeout = 100000; //设置请求超时时间
			def enableSSLflag = 0;
			def msgjson = Util.doHttpGet(url,charset,timeout,enableSSLflag);
			JSONObject obj = JSONObject.fromObject(msgjson);
			//log.debug("msgjson:"+msgjson);
			if(obj.get("status").equals("0")) {
				String[] data=new String[3];
				data[0] = "insert into sms_send(smsno,batch_id,sms_id,customer_guid,sk_invpty,invpty_name,phone,msg,state,priority,pipe,createdate,createby,creategp,stime,exno) select smsno,batch_id,sms_id,customer_guid,sk_invpty,invpty_name,phone,msg,1,priority,pipe,sysdate,createby,creategp,sysdate,exno from sms_send_task where smsno ='"+smsno+"'";
				data[1] = "update sms_send_batch sb set tosendsum=tosendsum+1 where batch_id='"+batch_id+"'";
				data[2] = "delete from sms_send_task where smsno ='"+smsno+"'";
				int[] sqlresult = eliteBean.updateBatch(data,"dataSource");
			}
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
		}
	}	
}